﻿using System.Linq;
using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Actions;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;

namespace CardPlay.DemoPack.Games.CanfieldVariants
{
    [Game("11CD14AC-3A9C-4F90-BE0A-0716BD052EE0", LevelOfDifficulty.Easy, LevelOfSkill.MostlyChance, DurationOfPlay.Within5To10Minutes, Family = FamilyName)]
    public class Goldfield : Canfield
    {
        public Goldfield()
        {
            TotalFoundations = 8;
        }

        protected override void PrepareDealScript(DealScript script)
        {
            script.DealAcesTo(StandardPileNames.Foundations.Take(4));
            script.DealRankTo(StandardPileNames.Foundations.Skip(4).Take(4), Rank.King);
            script.Deal("12 down,1 up").To(Reserve);
            script.DealCardToEach(NamesOfTableaus);
            script.DealRemainingFaceDownTo(Stock);
        }

        protected override bool AllowedToBuildFoundation(MovePileAction action)
        {
            var namesOfFirstFoundations = StandardPileNames.RangePatternForFoundations(1, 4);

            var buildingUp = action.TargetPile.Location.Matches(namesOfFirstFoundations);
            var buildLimit = buildingUp ? 7 : 6;

            return
                action.IsMovingSingleCard() && action.TargetPile.Count < buildLimit &&
                    (buildingUp 
                        ? action.IsBuilding(CardFaceSequence.SameSuitAscendingRank, Rank.Ace) 
                        : action.IsBuilding(CardFaceSequence.SameSuitDescendingRank, Rank.King));
        }

        protected override void OnTableauLayoutCreated(IPresentationContext presentation, CompositeLayout layout)
        {
            layout.HorizontalContentAlignment = HorizontalAlignment.Center;
        }
    }
}